﻿function newWin(url, width, height) {
    var leftm = Math.floor(screen.width * .1) + 30;
    var topm = Math.floor(screen.height * .05) + 30;
    window.open(url, "_blank", "height=" + height + ", width=" + width + ", toolbar=no, menubar=no, scrollbars=yes, resizable=yes, status=no, top=" + topm + ", left=" + leftm + ",location=no");
}

function newWin2(url, width, height) {
    var leftm = Math.floor(screen.width * .1) + 30;
    var topm = Math.floor(screen.height * .05) + 30;
    window.open(url, "_blank", "height=" + height + ", width=" + width + ", toolbar=no, menubar=no, scrollbars=yes, resizable=yes, status=no, top=" + topm + ", left=" + leftm + ",location=no");
}

function centerWin(url, w, h, name) {
    var top = (screen.availHeight - h) / 2;
    var left = (screen.availWidth - w) / 2;
    if (name == undefined) {
        name = "popwin";
    }
    var win = window.open(url, name, "height=" + h + ", width=" + w + ", top=" + top + ", left=" + left + ", toolbar=0, menubar=0, scrollbars=1, resizable=1,location=0, status=0");
    win.focus();
}

function centerWinNoScroll(url, w, h, name) {
    if (name == undefined) {
        name = "popwin";
    }
    var win = window.open(url, name, "height=" + screen.height + ", width=" + screen.width + ", top=0,left=0,toolbar=0,menubar=0,scrollbars=0,resizable=1,location=0, status=0");    
    win.moveTo(0, 0);
    win.focus();
}

function centerMaxWin(url, w, h, name) {
    if (name == undefined) {
        name = "popwin";
    }
    var win = window.open(url, name, "height=" + screen.height + ", width=" + screen.width + ", top=0,left=0,toolbar=0,menubar=0,scrollbars=1,resizable=1,location=0, status=0");
    win.moveTo(0, 0);
    win.focus();
}

function newWin(url) {
    var width = Math.floor(screen.width * .6);
    var height = Math.floor(screen.height * .5);
    var leftm = Math.floor(screen.width * .1) + 30;
    var topm = Math.floor(screen.height * .05) + 30;
    var argu = "toolbar=0,location=0,maximize=1,directories=0,status=1,menubar=0,scrollbars=1, resizable=1,left=" + leftm + ",top=" + topm + ", width=600px, height=540px"
    window.open(url, "", argu);
}

function showSubMenu(p_obj) {
    p_obj.className = "withSubMenu withSubMenuhover";
    var subMenuUl = $(p_obj).find("ul");
    subMenuUl[0].style.display = "block";
}

function hideSubMenu(p_obj) {
    p_obj.className = "withSubMenu";
    var subMenuUl = $(p_obj).find("ul");
    subMenuUl[0].style.display = "none";
}

//===杨正祎 沪江tab切换脚本,传入参数,this是要显示div的目标id，注意，目标id要有下划线,而且下划线之前的部分要和class一致。
function hjTab(p_objLink, p_strTargetId) {
    var _fatherTabList = $(p_objLink)[0].parentNode.parentNode;
    var _allTabA = $(_fatherTabList).find("a");
    var _allTabACount = _allTabA.length
    for (var _i = 0; _i < _allTabACount; _i++) {
        _allTabA[_i].className = "";
    }
    $(p_objLink).attr("className", "current");

    var targetClass = p_strTargetId.substring(0, p_strTargetId.indexOf('_'));
    targetClass = "." + targetClass;
    $(targetClass).css("display", "none");
    $("#" + p_strTargetId).css("display", "block");
}


function showTab(p_obj, p_str) {
    //修改选中tab样式
    var _fatherTabList = $(p_obj)[0].parentNode.parentNode;
    var _allTabA = $(_fatherTabList).find("a");
    var _i = 0;
    var _allTabACount = _allTabA.length
    for (var _i = 0; _i < _allTabACount; _i++) {
        _allTabA[_i].className = "";
    }
    $(p_obj)[0].className = "current";

    //显示对应tab的Div	
    var _strID = "#" + p_str;
    var _siblingTab = $(_strID).siblings(".tab");
    var _i = 0;
    var _siblingTabCount = _siblingTab.length
    for (var _i = 0; _i < _siblingTabCount; _i++) {
        _siblingTab[_i].style.display = "none";
    }
    $(_strID).css("display", "block");
}

/*新插入代码*/
function TextBoxEditor(atextArea) {
    this.element = atextArea;
}
TextBoxEditor.prototype.getValue = function () {
    return this.element.value;
}
TextBoxEditor.prototype.getSelection = function () {
    if (!!document.selection)
        return document.selection.createRange().text;
    else if (!!this.element.setSelectionRange)
        return this.element.value.substring(this.element.selectionStart, this.element.selectionEnd);
    else
        return false;
}
TextBoxEditor.prototype.replaceSelection = function (text) {
    var scrollTop = this.element.scrollTop;
    if (!!document.selection) {
        this.element.focus();
        $(this.element).attr('contentEditable',true); 
        var old = document.selection.createRange().text;
        var range = document.selection.createRange();
        range.text = text;
        range -= old.length - text.length;
    } else if (!!this.element.setSelectionRange) {
        var selection_start = this.element.selectionStart;
        this.element.value = this.element.value.substring(0, selection_start) + text + this.element.value.substring(this.element.selectionEnd);
        this.element.setSelectionRange(selection_start + text.length, selection_start + text.length);
    }
    this.element.focus();
    $(this.element).attr('contentEditable',true); 
    this.element.scrollTop = scrollTop;
}
TextBoxEditor.prototype.wrapSelection = function (before, after) {
    this.replaceSelection(before + this.getSelection() + after);
}
TextBoxEditor.prototype.insertBeforeSelection = function (text) {
    this.replaceSelection(text + this.getSelection());
}
TextBoxEditor.prototype.insertAfterSelection = function (text) {
    this.replaceSelection(this.getSelection() + text);
}
TextBoxEditor.prototype.injectEachSelectedLine = function (callback, before, after) {
    this.replaceSelection((before || '') + $A(this.getSelection().split("\n")).inject([], callback).join("\n") + (after || ''));
}

function showcolor2(color) {
    aTextBoxEditor.wrapSelection('[color=' + color + ']', '[/color]')
}
function showsize2(size) {
    aTextBoxEditor.wrapSelection('[size=' + size + ']', '[/size]')
}



function CountDown(nowT, endT, callback, rate) {
    this.end = false;
    var rate = rate || 500;

    var mistiming = nowT - new Date(); //计算本地时间跟服务器的时间差
    this.getSumMS = function () {
        return endT - new Date() - mistiming; //总的秒数
    };
    var id = 0;
    this.start = function () {
        var self = this;
        id = setInterval(function () {
            var sumMS = self.getSumMS();
            if (sumMS <= 0) {
                self.end = true;
                self.d = self.h = self.m = self.s = self.ms = 0;
                clearInterval(self.id);
                callback();
                return;
            }
            self.d = parseInt(sumMS / (1000 * 60 * 60 * 24));
            sumMS %= (1000 * 60 * 60 * 24);
            self.h = parseInt(sumMS / (1000 * 60 * 60));
            sumMS %= (1000 * 60 * 60);
            self.m = parseInt(sumMS / (1000 * 60));
            sumMS %= (1000 * 60);
            self.s = parseInt(sumMS / 1000);
            self.ms = sumMS % 1000;
            callback();
        }, rate);
    };
    this.stop = function () {
        clearInterval(id);
    }
}


function normal(id, times) {
    var obj = $("#" + id);
    obj.css("background-color", "#FFF");
    if (times < 0) {
        obj.focus();
        return;
    }
    times = times - 1;
    setTimeout("error('" + id + "'," + times + ")", 150);
}
function error(id, times) {
    var obj = $("#" + id);
    obj.css("background-color", "#F6CECE");
    times = times - 1;
    setTimeout("normal('" + id + "'," + times + ")", 150);
}
//开通课程
function doExchange(userid, classid) {
    if (userid <= 0) {
        window.location.href = "/?returnurl=/" + classid + "/exchange";
    } else {
        centerWin('/' + classid + '/exchange', 750, 500);
    }
    //window.event.returnValue = false;
    //return false;
}

//新版开通课程 by阿宝
function openClassNow(userId, classId) {
    if (userId > 0) {
        doExchangeBox(userId, classId);
    }
    else {
        login.doLogin(function () {
            removeFastLoginBox();
            doExchangeBox(userId, classId);
        });
    }    
}
//jBox新版购买窗口
function doExchangeBox(userId, classId) {
    $.jBox("<div style='padding:17px 0 0;'><iframe width='750' height='485' frameborder='0' scrolling='no' src='/" + classId + "/exchange'></iframe></div>",
    {
        width: 750,
        height: 540,
        title: "开通课程",
        draggable: false,
        buttons: {},
        closed: function () {
            if (userId <= 0) {
                document.location.reload();
            }
        }
    });
}
//移除快速登录框
function removeFastLoginBox() {
    $("#ulogin_mark_hj_login_" + $("#passport_login_frame").parent().attr("id").replace("hj_login_", "")).remove();
    $("#passport_login_frame").parent().remove();
}

var hjbox = {
    dialogCnt: "<div class='hjbox_winmedal-box' style='display:none;	position: fixed;_position: absolute;-moz-border-radius:5px;-webkit-border-radius: 5px;-khtml-border-radius: 5px;border:0;_border:2px solid #97ACD2;background:url(http://t.hujiang.com/images/transBG.png) no-repeat;_background:white;text-align: left;left: 50%;top: 50%;margin-top:-130px;margin-left: -196px;z-index: 999999;'><div style='background:#FFF;line-height: 22px;margin: 5px;padding: 12px 0 10px 5px;position: relative;top: -1px;'><span onclick=\"$(this).parents('.hjbox_winmedal-box').hide(300)\" style=\"position: absolute;right: 10px;top: 8px;width: 16px;height: 16px;text-indent: -10em;overflow: hidden;background: url(http://t.hujiang.com/images/ico_tip_win_close.gif) no-repeat;cursor: pointer;display:block;\">Close</span><div style='width: 250px;height: 35px;line-height:35px;padding-left:10px;'><img style='float:left; margin:4px 15px 0px 0px;' src='http://t.hujiang.com/images/ico_right_answer.gif' /> <span class='big gray'><!--content--></span></div></div></div>",
    showsuccess: function (cnt, autoTime) {
        $(".hjbox_winmedal-box").remove();
        $("body").append(this.dialogCnt.replace("<!--content-->", cnt).replace("ico_wrong_answer.gif", "ico_right_answer.gif"));
        $(".hjbox_winmedal-box").show(300);
        if (autoTime != null) {
            window.setTimeout(
			  function () {
			      $('.hjbox_winmedal-box').hide(300)
			  }, autoTime);
        }
    },
    showerror: function (cnt, autoTime) {
        $(".hjbox_winmedal-box").remove();
        $("body").append(this.dialogCnt.replace("<!--content-->", cnt).replace("ico_right_answer.gif", "ico_wrong_answer.gif"));
        $(".hjbox_winmedal-box").show(300);
        if (autoTime != null) {
            window.setTimeout(
			  function () {
			      $('.hjbox_winmedal-box').hide(300)
			  }, autoTime);
        }
    }
}

//字数检查，内容闪烁
var AlertHelper = {
    checkCount: function (length) {
        var length = $.trim($(obj).val().replace(/\n/g, "").replace(/[^\x00-\xff]/g, "rr")).length;
        var strLength = parseInt($.trim($(obj).val().replace(/\n/g, "").replace(/[^\x00-\xff]/g, "rr")).length / 2);
        if (strLength > length || length == 0) {
            return false;
        }
        return true;
    },
    flickCnt: function (obj) {
        var showCount = 0;

        var MyInterval = setInterval(function () {
            if (showCount < 4) {
                if (showCount % 2 == 0) {
                    $(obj).addClass("empty");
                }
                else {
                    $(obj).removeClass("empty");
                }
                showCount++;
            }
            else {
                clearInterval(MyInterval);
            }
        }, 150);
    }
}